<?php
/**
 * File: calendarcontroller.php.
 * Author: zhouzhe@haibao.com
 * Editor: PhpStorm
 * Date: 16/3/23 下午6:11
 * Brief:
 */
namespace haibao\cms\business\control\pc\channel\calendar;
use haibao\cms\business\Calendar as CalendarBusiness;

class CalendarController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
	const CALENDAR_COMMENT = 482;
	private $calenderData = array();

	private $articleIds = array();

	private $recommendProducts = array();


	private static $week = array(
		1=>'MON',
		2=>'TUES',
		3=>'WED',
		4=>'THUR',
		5=>'FRI',
		6=>'SAT',
		7=>'SUN'
	);

	public function __construct($updateTime='',$controlId='',$isPublish=''){
		parent::__construct($updateTime,$controlId,$isPublish);
	}

	public function displayTemplateHtml(){
		$content = $this->getParameter(self::$CONTROL_CONTENT);

		$calendarIndex = CalendarBusiness::getCalendarIndex();
		$this->getData($calendarIndex);

		$path = $this->generateHtmlPath(__CLASS__);
		$isNewHtml = $this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		return $content;
	}

	private function getData($calendarIndex){
		$this->calenderData = $calendarIndex;
		$this->getRecommendProducts();
		$this->getDateFormat();
		$this->getProductByCalendarIds();
		$this->getProImagesForCalendar();
		$this->getHotImageForCalendar();
		$this->getArticleTags();

		$this->calenderData = array_values($this->calenderData);
		if(isset($this->calenderData[0])){
			$this->assign('currentDate',$this->calenderData[0]['PublishTime']->format('Y-m-d'));
		}else{
			$this->assign('currentDate',date('Y-m-d',time()));
		}
		$this->assign('calendarData',array_values($this->calenderData));
		$this->assign('recommendProducts',$this->recommendProducts);
		$hiddenDatas = $this->getThisMonthDateInCalendarData();
		$this->assign('monthCalendarData',$hiddenDatas[0]);
		$this->assign('hiddenCalendarData',$hiddenDatas[1]);
		$this->assign('calendarObjId',self::CALENDAR_COMMENT);
		$this->assign('commentTypeCode',\haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE);
		$this->assign('commentHost',\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_COMMENT));
		$this->assign('moreCalendatUrl',\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_CALENDAR).'/list/');

	}

	private function getDateFormat(){
		foreach($this->calenderData as $key=>$data){
			$this->calenderData[$key]['Mon'] = strtoupper($data['PublishTime']->format('M'));
			$this->calenderData[$key]['Day'] = $data['PublishTime']->format('d');
			$this->calenderData[$key]['Week'] = self::$week[$data['PublishTime']->format('N')];
			$this->calenderData[$key]['ArticleUrl'] = \haibao\classlibrary\cms\Config::getConfig
				(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_CALENDAR).'/article/'.$data['PublishId'].'.htm';
		}
	}

	/**
	 * 获取造型日历的单品
	 */
	private function getProductByCalendarIds(){
		$calendarIds = array_keys($this->calenderData);
		$products = CalendarBusiness::getCalendarProductsByCalendarIds($calendarIds);
		foreach($products as $key=>$pro){
			$this->calenderData[$key]['Product'] = $pro;
		}
	}

	/**
	 * 获取造型日历文章的导读图
	 */
	private function getProImagesForCalendar(){
		$articleIds = $tmpArr = array();
		foreach($this->calenderData as $key=>$item){
			$articleIds[] = $item['ArticleId'];
			$tmpArr[$item['ArticleId']] = $key;
		}
		$this->articleIds = $articleIds;
		$control = \haibao\cms\business\control\ArticleController::initArticleControl();
		$proImages = $control->getArticleProImage($articleIds, \haibao\cms\model\data\ArticleProImage::Level_SIX);
		$resArr = array();
		foreach ($proImages as $key=>$image) {
			$resArr[$key] = self::getImageLink($image[0],300,400,90,0);
			$this->calenderData[$tmpArr[$key]]['ProImage'] = $resArr[$key];

		}
	}

	private function getHotImageForCalendar(){
		$imageIds = $tmpArr = $imageUrls = array();
		foreach($this->calenderData as $key=>$item){
			$imageIds[] = $item['HotImageId'];
			$tmpArr[$item['HotImageId']] = $key;
		}
		$imageUrls = \haibao\cms\business\ImageLibrary::getImageUrlById($imageIds);

		foreach($imageUrls as $key=>$value){
			$imageUrls[$key] = self::getImageLink($value, 300, 300, 90, 0);
			$this->calenderData[$tmpArr[$key]]['HotImage'] = $imageUrls[$key];
		}
	}

	private function getArticleTags(){
		$articleKeywords = \haibao\cms\business\ArticleTag::getTagsByArticleIds($this->articleIds);
		foreach($this->calenderData as $key=>$data){
			$this->calenderData[$key]['Tags'] = array();
			if(isset($articleKeywords[$data['ArticleId']])){
				$this->calenderData[$key]['Tags'] = $articleKeywords[$data['ArticleId']];
			}
		}
	}

	private function getRecommendProducts(){
		$calendarData = array_keys($this->calenderData);
		$calendarId = $calendarData[0];
		$this->recommendProducts = CalendarBusiness::getRecommendProductsByCalendarId($calendarId);
		if($this->recommendProducts){
			$imageIds = $tmpArr = $imageUrls = array();
			foreach($this->recommendProducts as $key=>$item){
				$this->recommendProducts[$key]['ProductUrl'] =  \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS)
					.'/product/'.$item['PublishId'].'/';
				$imageIds[] = $item['PicId'];
				$tmpArr[$item['PicId']] = $key;
			}
			$imageUrls = \haibao\cms\business\ImageLibrary::getImageUrlById($imageIds);

			foreach($imageUrls as $key=>$value){
				$imageUrls[$key] = self::getImageLink($value, 160, 160, 90, 0);
				$this->recommendProducts[$tmpArr[$key]]['PicId'] = $imageUrls[$key];
			}
		}
	}

	private function getThisMonthDateInCalendarData(){
		$hiddenCalendarData = array();
		$dateStr = '';
		$thisMonth = Date('Y-n',time());
		foreach($this->calenderData as $data){
			if($data['PublishTime']->format('Y-n') == $thisMonth){
				$hiddenCalendarData[$data['PublishTime']->format('j')]['Img'] = $data['ProImage'];
				$hiddenCalendarData[$data['PublishTime']->format('j')]['Title'] = $data['ShortTitle'];
				$hiddenCalendarData[$data['PublishTime']->format('j')]['Url'] = $data['ArticleUrl'];
				$dateStr .= $data['PublishTime']->format('Y-n-j').',';
			}
		}
		return array($dateStr,$hiddenCalendarData);
	}
}